package org.bilinc.gz001;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.util.*;
import java.util.List;

public class YearHeaderRenderer extends DefaultTableCellRenderer {
    private List<Integer> mHolidays;

    public YearHeaderRenderer() {
        setHorizontalAlignment(SwingConstants.CENTER);
        setOpaque(true);
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
    }

    public void updateUI() {
        super.updateUI();
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
                                                   boolean selected, boolean focused, int row, int column) {
        final JTableHeader h = (table != null) ? table.getTableHeader() : null;

        if (h != null) {
            if (((column + 2 > 6) &&
                    (column + 2) % 7 == 0) || column + 1 == 6) {
                setBackground(Color.YELLOW);
            } else {
                setBackground(UIManager.getColor("TableHeader.background"));
            }

            if (mHolidays != null) {
                if (mHolidays.contains(column)) {
                    setBackground(Color.RED);
                }
            }
        }
        setValue(value);
        return this;
    }

    public void setHolidays(final List<Integer> h) {
        if (h == null) {
            System.out.println("YearRenderer setHolidays null");
            return;
        }
        if (!h.isEmpty()) {
            mHolidays = h;
        }
    }
}